package DAOS;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Hibernate.HibernateUtil;
import Negocio.Medico;
import Negocio.Planilla;

public class PlanillaDAO {
	private static PlanillaDAO instancia = null;
	
	private static SessionFactory sf = null;

	public static PlanillaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new PlanillaDAO();
		} 
		return instancia;
	}
	
	public void guardarEspecialidad(Planilla p){

		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(p);
		session.flush();
		session.getTransaction().commit();
		session.close();

	}


	public void eliminarEspecialidad(Planilla p){

		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(p);
		session.flush();
		session.getTransaction().commit();
		session.close();
		

	}


	public void modificarEspecialidad(Planilla p){

		Session session = sf.openSession();
		session.beginTransaction();
		session.update(p);
		session.flush();
		session.getTransaction().commit();
		session.close();

	}


	public List<Object[]> listarPlanillas() {

		Session session = sf.openSession();
		Query q = session.createQuery
		(
				"SELECT e.nombre" +
				"FROM Especialidad e"
		);
		
		@SuppressWarnings("unchecked")
		List<Object[]> planillas = new ArrayList<Object[]>(q.list());
		session.close();
		return planillas;
		
	}
	
	
	@SuppressWarnings("unchecked")
	public List<List<Planilla>> cargarPlanillas () {
		Session session = sf.openSession();
		
		Query maxID = session.createQuery(
				"SELECT MAX (p.idPlanilla)" +
				"FROM Planilla p"
		);
		
		List<Planilla> p = new ArrayList<Planilla>();
		@SuppressWarnings("rawtypes")
		List<List<Planilla>> pp = new ArrayList();
		long max = maxID.executeUpdate();
		for (int i=0 ; i<max ; i++){	
			Query q = session.createQuery(
					"SELECT" +
					"FROM Planilla p" +
					"WHERE idPlanilla = " + i
			);
			p.addAll(q.list());
			pp.add(p);
		}
		
		session.close();
		
		return pp;
	}
	public Planilla cargarUnaPlanilla (String nombreOS) {
		return new Planilla();
		
	}
}
